package com.lm.jsonpath.example;

import com.jayway.jsonpath.JsonPath;
import java.util.List;
import java.util.Map;

public class JSONPathDemo {
    public static void main(String[] args) {
        String json ="{\n" +
                "  \"store\": {\n" +
                "    \"book\": [\n" +
                "      { \"category\": \"fiction\", \"author\": \"John\", \"price\": 10.99 },\n" +
                "      { \"category\": \"science\", \"author\": \"Jane\", \"price\": 12.99 },\n" +
                "      { \"category\": \"fiction\", \"author\": \"George\", \"price\": 8.99 }\n" +
                "    ],\n" +
                "    \"bicycle\": {\n" +
                "      \"color\": \"red\",\n" +
                "      \"price\": 99.99\n" +
                "    }\n" +
                "  }\n" +
                "}";

        List<String> categories = JsonPath.read(json, "$.store.book[*].category");
        System.out.println("📚书籍类别: " + categories);

        List<Map<String, Object>> expensiveBooks = JsonPath.read(json, "$.store.book[?(@.price > 10)]");
        System.out.println("💰价格大于 10 的书籍: " + expensiveBooks);

        List<Double> prices = JsonPath.read(json, "$.store..price");
        System.out.println("💵所有价格: " + prices);

        String color = JsonPath.read(json, "$.store.bicycle.color");
        System.out.println("🚲自行车颜色: " + color);


        String category = "fiction";
        String jsonPath = String.format("$.store.book[?(@.category == '%s')]", category);
        List<Map<String, Object>> result = JsonPath.read(json, jsonPath);
        System.out.println("🔍查询结果: " + result);
    }
}
